package com.cms.web.business;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.biz.FlowManagerServiceMgr;
import com.cms.persistence.contractManager.accreditationContract.PSHTMX;
import com.cms.web.pub.EMPUtil;
import com.common.util.DateUtil;
import com.dal.common.sequence.UUIDGenerator;
import com.metadata.biz.MetaDataServiceMgr;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;
import com.persistence.qxgl.beans.SYSUser;
import com.pub.util.EMPJsonUtil;
import com.web.BaseController;

import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.meta.bizmodel.SYS_MDL_CTN;
import com.efounder.dict.DMTServiceMgr;
import com.efounder.dict.FCTServiceMgr;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.server.provider.FormModelDataProvider;
import com.efounder.form.server.resolver.util.FormModelDataResolverUtil;

/** 
 * 评审合同控制器
 * 
 * @author ES
 * @version 1.0
 */
@Controller
@RequestMapping(value="/contractManager")
public class AccreditationContractController extends BaseController{
	@Autowired
	private                            DMTServiceMgr                                   DMTService;
	
	@Autowired
	private                            FCTServiceMgr                                   FCTService;
	
	@Autowired
	private                       MetaDataServiceMgr                           metaDataServiceMgr;
	
	@Autowired
	private                    FlowManagerServiceMgr                           FlowManagerService;
	
	@Autowired
	private                FormModelDataResolverUtil                            formModelResolver;
	
	@Autowired
	private                    FormModelDataProvider                                formModelUtil;
	
	@Autowired
	public                               HttpSession                                      session;

	/**
	 * 评审合同列表
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/accreditationContract")
	public String accreditationContractList(Model model) throws Exception {	
		EFDataSet       ndDataset = DMTService.searchDictRow("HTKJND", "F_NDBH", "", "1", 0, 100);
		Calendar              cal = Calendar.getInstance();
		int                  year = cal.get(Calendar.YEAR);
		JParamObject           PO = JParamObject.Create();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		
		model.addAttribute("ndDataset", ndDataset);
		model.addAttribute("F_YEAR", year);
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName("BILL_WHERE", " F_KJND = '" + year + "' and F_CODE = '" + user.getUSER_ORGID() + "' and F_DJZT = '1'");
		PO.SetValueByParamName("BILL_ORDER", " desc");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		EFFormDataModel formDataModel = formModelUtil.loadForm(PO);
		model.addAttribute("dataset", formDataModel.getBillDataSet());
		model.addAttribute("F_DJZT", "1");
		return "/cms/business/accreditationContract/list";
	}
	
	/**
	 * 评审合同列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/accreditationContract/list")
	public String accreditationContractList(@RequestParam String F_KJND, @RequestParam String F_DJZT, Model model) throws Exception {		
		EFDataSet       ndDataset = DMTService.searchDictRow("HTKJND", "F_NDBH", "", "1", 0, 100);
		JParamObject           PO = JParamObject.Create();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		String         BILL_WHERE = " F_KJND = '" + F_KJND + "' and F_CODE = '" + user.getUSER_ORGID() + "'";
		
		if(!F_DJZT.equals("-1")) {
			BILL_WHERE += " and F_DJZT = '" + F_DJZT + "'";
		}
		
		model.addAttribute("ndDataset", ndDataset);
		model.addAttribute("F_YEAR", F_KJND);
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		PO.SetValueByParamName("BILL_ORDER", " desc");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		EFFormDataModel formDataModel = formModelUtil.loadForm(PO);
		model.addAttribute("dataset", formDataModel.getBillDataSet());
		model.addAttribute("F_DJZT", F_DJZT);
		return "/cms/business/accreditationContract/list";
	}
	
	/**
	 * 评审合同添加
	 * @param model
	 * @return
	 */
	@RequestMapping("/accreditationContract/add")
	public String accreditationContractAdd(Model model) {	
		SYSUser                  user = (SYSUser) session.getAttribute("contextUser");
		String                 F_GUID = UUIDGenerator.nextId();
		String                 F_PSNR = "";
		EFDataSet             dataset = EFDataSet.getInstance();
		EFRowSet               rowset = EFRowSet.getInstance();
		EFRowSet                 psRS = null;
		String                strDate = DateUtil.date3String(DateUtil.PATTERN_DATE);
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		EFRowSet          nodeRowset = EFRowSet.getInstance();
		JSONObject        jsonObject = new JSONObject();
		
		dataset = DMTService.searchDictRow("PSSXZD", "F_PSBH", "", "1", 0, 100);
		for(int i = 0; i < dataset.getRowCount(); i++) {
			psRS = dataset.getRowSet(i);
			F_PSNR += psRS.getString("F_PSMS", "") + "\r\n";
		}
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "PSHT_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_TYPE", "start");
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);

		if(flowNodeDS == null || flowNodeDS.getRowCount() == 0) {
			rowset.putString("FLOW_NODE_NAME", "提交任务");
		} else {
			nodeRowset = flowNodeDS.getRowSet(0);
			rowset.putString("FLOW_NODE_NAME", nodeRowset.getString("NODE_NAME", ""));
		}
		
		if(flowNodeDS != null && flowNodeDS.getRowCount() > 0) {
			nodeRowset = flowNodeDS.getRowSet(0);
			jsonObject.putAll(nodeRowset);
			model.addAttribute("nodeRowSet", nodeRowset);
			model.addAttribute("jsonNodeRowSet", jsonObject.toString());
			model.addAttribute("canEdit", "1");
		} else {
			nodeRowset.putString("FLOW_NODE_NAME", "提交任务");
			model.addAttribute("canEdit", "0");
			model.addAttribute("canSubmit", "0");
			model.addAttribute("canRetrieve", "0");
			model.addAttribute("canRollback", "0");
		}
		
		rowset.putString("F_KJND", DateUtil.date3String("yyyy"));
		rowset.putString("F_GUID", F_GUID);
		rowset.putString("F_CODE", user.getUSER_ORGID());
		rowset.putString("F_ZDR", user.getUSER_ID());
		rowset.putString("F_ZDRMC", user.getUSER_NAME());
		rowset.putString("F_PSSX", F_PSNR);
		rowset.putString("F_ZDRQ", strDate);
		rowset.putString("F_PSSJ", strDate);
		rowset.putString("F_DJZT", "1");
		rowset.putString("F_CODE", user.getUSER_ORGID());
		model.addAttribute("dictObject", rowset);
		return "/cms/business/accreditationContract/form";
	}
	
	/**
	 * 评审合同添加保存
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/accreditationContract/update", method=RequestMethod.POST)
	public  ModelAndView accreditationContractInsert(@RequestBody Map dataMap, Model model) throws Exception {  
		EFRowSet              rowSet = null;
		String               strNode = "";
		EFRowSet          metaRowSet = null;
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();		
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		EFRowSet          nodeRowset = EFRowSet.getInstance();
		SYSUser                 user = (SYSUser) session.getAttribute("contextUser");		
		EFRowSet          nodeRowSet = null;
		
		
		strNode = (String) dataMap.get("nodeRowSet");                            //将节点信息提取出来
		dataMap.remove("nodeRowSet");                                            //把数据集中的节点信息删除
		metaRowSet = EMPUtil.getDictsMetaRowSet(metaDataServiceMgr, dataMap);    //取分录表的元数据
		nodeRowSet = EMPJsonUtil.convertJsonToRowSet(strNode);                   //将节点JSON转化为EFRowSet
		//将json数据转化为EFDataSet
		rowSet = EMPJsonUtil.convertMapToRowSet(dataMap, metaRowSet);            //将主数据JSON转化为EFRowSet
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "PSHT_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_ID", nodeRowSet.getString("NODE_ID", ""));
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);

		if(flowNodeDS == null || flowNodeDS.getRowCount() == 0) {
			return ajaxDoneError("提交失败！原因：<br>【" + user.getUSER_ID() + "-" + user.getUSER_NAME() + "】没有权限提交单据！");
		} else {
			nodeRowset = flowNodeDS.getRowSet(0);
		}
		
		rowSet.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEMS_ALL_);

		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "CMSSaveFormToFlow");
		PO.SetValueByParamName("BILL_WHERE", " F_GUID = '" + rowSet.getString("F_GUID", "") + "'");
		PO.SetValueByParamName("FLOW_ID", "PSHT_FLOW");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NOTE, rowSet.getString("F_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowSet.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_BMBH, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER, user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "pending");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "create");
		
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NOTE, rowSet.getString("F_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "01", rowSet.getString("F_KHBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "02", rowSet.getString("F_KHMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "03", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "04", rowSet.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "05", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "06", rowSet.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "07", rowSet.getString("F_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "08", rowSet.getString("F_FSCS", ""));
		
		EFFormDataModel formDataModel = formModelResolver.saveForm("PSHTXX", rowSet, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("保存失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowSet = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowSet.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowSet.getObject("F_DJBH", ""));
		return ajaxDoneSuccess("单据分录保存成功！单据编号【" + rowSet.getObject("F_DJBH", "") + "】"); 
	}
	
	/**
	 * 评审合同添加保存
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/accreditationContract/submit", method=RequestMethod.POST)
	public  ModelAndView accreditationContractSubmit(@RequestBody Map dataMap, Model model) throws Exception {  
		EFRowSet              rowSet = EMPJsonUtil.convertMapToRowSet(dataMap);
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		EFRowSet          nodeRowset = EFRowSet.getInstance();
		SYSUser                 user = (SYSUser) session.getAttribute("contextUser");
		EFFormDataModel    formModel;
		String               strNode = (String) dataMap.get("nodeRowSet");                            //将节点信息提取出来
		dataMap.remove("nodeRowSet");                                                                 //把数据集中的节点信息删除
		EFRowSet          nodeRowSet = EMPJsonUtil.convertJsonToRowSet(strNode);                      //将节点JSON转化为EFRowSet
		String               OP_NOTE = "";
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "PSHT_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_ID", nodeRowSet.getString("NODE_ID", ""));
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);
		
		if(flowNodeDS == null || flowNodeDS.getRowCount() == 0) {
			return ajaxDoneError("提交失败！原因：<br>【" + user.getUSER_ID() + "-" + user.getUSER_NAME() + "】没有权限提交单据！");
		} else {
			nodeRowset = flowNodeDS.getRowSet(0);
		}
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + rowSet.getString("F_GUID", "") + "'");
		formModel = formModelUtil.loadForm(PO);
		EFRowSet headRowset = formModel.getBillDataSet().getRowSet(0);
		headRowset.putString("F_CHDATE", rowSet.getString("F_CHDATE", ""));
		
		PO = JParamObject.Create();
		
		OP_NOTE = rowSet.getString("OP_NOTE", "");
		if(OP_NOTE == null || OP_NOTE.trim().equals("")) {
			OP_NOTE = rowSet.getString("F_NOTE", "");
		}
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NOTE, OP_NOTE);
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "01", rowSet.getString("F_KHBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "02", rowSet.getString("F_KHMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "03", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "04", rowSet.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "05", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "06", rowSet.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "07", rowSet.getString("F_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "08", rowSet.getString("F_FSCS", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "09", rowSet.getString("F_ZDR", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "10", rowSet.getString("F_ZDRMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "11", user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "12", user.getUSER_NAME());
		
		PO.SetValueByParamName(FLOW_TASK_LIST._F_DATE + "01", rowSet.getString("F_ZDRQ", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_DATE + "02", DateUtil.date3String("yyyy-MM-dd"));
		
		headRowset.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEM_);
		
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "CMSSubmitFormToFlow");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		
		PO.SetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "PSHT_FLOW");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE_TYPE, nodeRowset.getString("NODE_TYPE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, nodeRowset.getString("NODE_NAME", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_BMBH, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER, user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "pending");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "submit");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "submit");
		
		EFFormDataModel formDataModel = formModelResolver.saveForm("PSHTXX", headRowset, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("单据提交失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowSet = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowSet.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowSet.getObject("F_DJBH", ""));
		model.addAttribute("OP_NODE_NEXT", PO.GetValueByParamName("OP_NODE_NEXT", ""));
		return ajaxDoneSuccess("单据提交成功！"); 
	}
	
	/**
	 * 评审合同添加保存
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/accreditationContract/retake", method=RequestMethod.POST)
	public  ModelAndView accreditationContractRetake(@RequestBody Map dataMap, Model model) throws Exception {  
		EFRowSet              rowSet = EMPJsonUtil.convertMapToRowSet(dataMap);
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		EFRowSet          nodeRowset = EFRowSet.getInstance();
		SYSUser                 user = (SYSUser) session.getAttribute("contextUser");
		EFFormDataModel    formModel;
		String               strNode = (String) dataMap.get("nodeRowSet");                            //将节点信息提取出来
		dataMap.remove("nodeRowSet");                                                                 //把数据集中的节点信息删除
		EFRowSet          nodeRowSet = EMPJsonUtil.convertJsonToRowSet(strNode);                      //将节点JSON转化为EFRowSet
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "PSHT_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_ID", nodeRowSet.getString("NODE_ID", ""));
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);

		if(flowNodeDS == null || flowNodeDS.getRowCount() == 0) {
			return ajaxDoneError("提交失败！原因：<br>【" + user.getUSER_ID() + "-" + user.getUSER_NAME() + "】没有权限提交单据！");
		} else {
			nodeRowset = flowNodeDS.getRowSet(0);
		}
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + rowSet.getString("F_GUID", "") + "'");
		formModel = formModelUtil.loadForm(PO);
		EFRowSet headRowset = formModel.getBillDataSet().getRowSet(0);
		headRowset.putString("F_CHDATE", rowSet.getString("F_CHDATE", ""));
		
		PO = JParamObject.Create();
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NOTE, rowSet.getString("F_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "01", rowSet.getString("F_KHBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "02", rowSet.getString("F_KHMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "03", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "04", rowSet.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "05", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "06", rowSet.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "07", rowSet.getString("F_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "08", rowSet.getString("F_FSCS", ""));
		
		headRowset.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEM_);
		
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "CMSSubmitFormToFlow");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		
		PO.SetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "PSHT_FLOW");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE_TYPE, nodeRowset.getString("NODE_TYPE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, nodeRowset.getString("NODE_NAME", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_BMBH, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER, user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "pending");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "retake");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "retake");
		
		EFFormDataModel formDataModel = formModelResolver.saveForm("PSHTXX", headRowset, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("单据提交失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowSet = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowSet.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowSet.getObject("F_DJBH", ""));
		return ajaxDoneSuccess("单据提交成功！"); 
	}
	

	/**
	 * 取回评审合同
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/accreditationContract/rollback", method=RequestMethod.POST)
	public  ModelAndView accreditationContractRollback(@RequestBody Map dataMap, Model model) throws Exception {  
		EFRowSet              rowSet = EMPJsonUtil.convertMapToRowSet(dataMap);
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		EFRowSet          nodeRowset = EFRowSet.getInstance();
		SYSUser                 user = (SYSUser) session.getAttribute("contextUser");
		EFFormDataModel    formModel;
		String               strNode = (String) dataMap.get("nodeRowSet");                            //将节点信息提取出来
		dataMap.remove("nodeRowSet");                                                                 //把数据集中的节点信息删除
		EFRowSet          nodeRowSet = EMPJsonUtil.convertJsonToRowSet(strNode);                      //将节点JSON转化为EFRowSet
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "PSHT_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_ID", nodeRowSet.getString("NODE_ID", ""));
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);

		if(flowNodeDS == null || flowNodeDS.getRowCount() == 0) {
			return ajaxDoneError("提交失败！原因：<br>【" + user.getUSER_ID() + "-" + user.getUSER_NAME() + "】没有权限提交单据！");
		} else {
			nodeRowset = flowNodeDS.getRowSet(0);
		}
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + rowSet.getString("F_GUID", "") + "'");
		formModel = formModelUtil.loadForm(PO);
		EFRowSet headRowset = formModel.getBillDataSet().getRowSet(0);
		headRowset.putString("F_CHDATE", rowSet.getString("F_CHDATE", ""));
		
		PO = JParamObject.Create();
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NOTE, rowSet.getString("OP_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "01", rowSet.getString("F_KHBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "02", rowSet.getString("F_KHMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "03", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "04", rowSet.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "05", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "06", rowSet.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "07", rowSet.getString("F_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "08", rowSet.getString("F_FSCS", ""));

		headRowset.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEM_);
		
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "CMSSubmitFormToFlow");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		
		PO.SetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "PSHT_FLOW");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE_TYPE, nodeRowset.getString("NODE_TYPE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, nodeRowset.getString("NODE_NAME", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_BMBH, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER, user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "pending");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "rollback");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "rollback");
		
		EFFormDataModel formDataModel = formModelResolver.saveForm("PSHTXX", headRowset, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("单据提交失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowSet = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowSet.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowSet.getObject("F_DJBH", ""));
		return ajaxDoneSuccess("单据提交成功！"); 
	}
	
	/**
	 * 评审合同编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/printAccreditationContract/print/{F_GUID}")
	public String printAccreditationContract(@PathVariable("F_GUID") String F_GUID, Model model) {
		String[]            array = null;	
		String           tempPssx = "";
		JParamObject           PO = JParamObject.Create();
		EFFormDataModel formModel;
		String         BILL_WHERE = "";
		EFRowSet       itemRowset = null;
		JParamObject       flowPO = JParamObject.Create();
		EFRowSet       flowRowSet = null;
		DateFormat         format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		Long                lTime = null;
		Date                 date = new Date();
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + F_GUID + "'");
		
		try {
			formModel = formModelUtil.loadForm(PO);
			EFDataSet     itemCTN = metaDataServiceMgr.getSYS_MDL_CTN("PSHTModel", SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_);
			EFRowSet       rowset = formModel.getBillDataSet().getRowSet(0);
			EFDataSet     dataset = rowset.getDataSet(itemCTN.getRowSet(0).getString("CTN_FCT1", ""));
			EFDataSet      pssxDS = EFDataSet.getInstance();
			EFRowSet       pssxRS = null;
			String         F_PSSX = rowset.getString("F_PSSX", "");
			tempPssx = F_PSSX.replaceAll("\n", "@");
			array = tempPssx.split("@");
			for(int i = 0; i < array.length; i++) {
				pssxRS = EFRowSet.getInstance();
				pssxRS.putString("F_PSSX", array[i]);
				pssxDS.insertRowSet(pssxRS);
			}
			
			BILL_WHERE = " BIZ_DATE = '" + rowset.getString("F_KJND", "") + "' and BIZ_DJBH = '" + rowset.getString("F_DJBH", "") + "' and "
	           		   + " FLOW_ID = 'PSHT_FLOW' and NODE_SRC = 'N2' and NODE_TAG = 'N3' and RESR_IN_CAUSE = 'submit'";
			flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
			flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
			flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
			flowRowSet = FCTService.getFCTRow(flowPO);
	
			if(flowRowSet == null) {					
				flowPO.SetValueByParamName("tableName", "FLOW_TASK_END_LIST");
				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
				flowRowSet = FCTService.getFCTRow(flowPO);
			}
			if(flowRowSet != null) {
				lTime = Long.parseLong(flowRowSet.getString("OP_ID", ""));
				date.setTime(lTime);
				rowset.putString("F_PSRQ", format.format(date));
				rowset.putString("F_PSJL", flowRowSet.getString("OP_NOTE", ""));
				rowset.putString("F_JLSP", flowRowSet.getString("OP_USER", ""));
				rowset.putString("F_JLSPMC", flowRowSet.getString("OP_USER_NAME", ""));
			}
//	
//			for(int i = 0; i < dataset.getRowCount(); i++) {
//				itemRowset = dataset.getRowSet(i);
//				//获取流程信息
//				BILL_WHERE = " BIZ_DATE = '" + rowset.getString("F_KJND", "") + "' and BIZ_DJBH = '" + rowset.getString("F_DJBH", "") + "' and "
//				           + " FLOW_ID = 'PSHT_FLOW' and NODE_SRC = 'N2' and NODE_TAG = 'N3' and RESR_OUT_CAUSE = 'submit' and OP_BMBH = '" + itemRowset.getString("F_BMBH", "") + "'";
//				flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
//				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
//				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
//				flowRowSet = FCTService.getFCTRow(flowPO);
//				
//				if(flowRowSet == null) {					
//					flowPO.SetValueByParamName("tableName", "FLOW_TASK_END_LIST");
//					flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
//					flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
//					flowRowSet = FCTService.getFCTRow(flowPO);
//				}
//				if(flowRowSet != null) {
//					itemRowset.putString("F_BMSP", flowRowSet.getString("OP_USER", ""));
//					itemRowset.putString("F_BMSPMC", flowRowSet.getString("OP_USER_NAME", ""));
//				}
//			}
			model.addAttribute("pssxDS", pssxDS);
			model.addAttribute("dictObject", rowset);
			model.addAttribute("dataset", dataset);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/cms/business/accreditationContract/printForm";
	}
	
	/**
	 * 评审合同编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/accreditationContract/edit/{F_GUID}")
	public String accreditationContractEdit(@PathVariable("F_GUID") String F_GUID, @RequestParam String FLOW_NODE, Model model) {
		JParamObject           PO = JParamObject.Create();
		JParamObject   flowNodePO = JParamObject.Create();
		JParamObject       flowPO = JParamObject.Create();
		JParamObject     searchPO = JParamObject.Create();
		EFFormDataModel formModel;
		EFDataSet      flowNodeDS = null;
		EFDataSet  nextFlowNodeDS = null;
		EFRowSet       nodeRowset = EFRowSet.getInstance();
		EFRowSet   nextFodeRowset = EFRowSet.getInstance();
		EFDataSet     flowDataSet = EFDataSet.getInstance();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		String         BILL_WHERE = "";
		EFRowSet       flowRowset = null;
		EFRowSet   nextFlowRowset = EFRowSet.getInstance();
		JSONObject     jsonObject = new JSONObject();
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + F_GUID + "'");
		
		//获取流程节点
		flowNodePO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowNodePO.SetValueByParamName("FLOW_ID", "PSHT_FLOW");
		flowNodePO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowNodePO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowNodePO.SetValueByParamName("NODE_ID", FLOW_NODE);
		flowNodePO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowNodePO);		
		
		try {
			//加载模型数据
			PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
			PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
			PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + F_GUID + "'");
			formModel = formModelUtil.loadForm(PO);
			EFDataSet itemCTN = metaDataServiceMgr.getSYS_MDL_CTN("PSHTModel", SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_);
			EFRowSet rowset = formModel.getBillDataSet().getRowSet(0);
			EFDataSet dataset = rowset.getDataSet(itemCTN.getRowSet(0).getString("CTN_FCT1", ""));
			
			if(flowNodeDS != null && flowNodeDS.getRowCount() > 0) {
				nodeRowset = flowNodeDS.getRowSet(0);
				//如果不是结束节点，则查询下一个节点
				if(!nodeRowset.getString("NODE_TYPE", "").equals("end")) {
					//查询下一个节点
					searchPO.SetValueByParamName("tableName", "FLOW_NODE_LIST");
					searchPO.SetValueByParamName("sqlWhere", "FLOW_ID = 'PSHT_FLOW' and NODE_ID = '" + nodeRowset.getString("NODE_NEXT", "") + "'");
					searchPO.SetValueByParamName("searchCol", "NODE_ID");
					nextFlowNodeDS = DMTService.searchDictRow(searchPO);
					if(nextFlowNodeDS != null && nextFlowNodeDS.getRowCount() > 0) {
						nextFodeRowset = nextFlowNodeDS.getRowSet(0);
						nodeRowset.putString("NODE_NEXT_TYPE", nextFodeRowset.getString("NODE_TYPE", ""));
					}
				}
				//查询上一个节点
				searchPO.SetValueByParamName("tableName", "FLOW_NODE_LIST");
				searchPO.SetValueByParamName("sqlWhere", "FLOW_ID = 'PSHT_FLOW' and NODE_NEXT = '" + nodeRowset.getString("NODE_ID", "") + "'");
				searchPO.SetValueByParamName("searchCol", "NODE_ID");
				nextFlowNodeDS = DMTService.searchDictRow(searchPO);
				if(nextFlowNodeDS != null && nextFlowNodeDS.getRowCount() > 0) {
					nextFodeRowset = nextFlowNodeDS.getRowSet(0);
					nodeRowset.putString("NODE_PRE", nextFodeRowset.getString("NODE_ID", ""));
					nodeRowset.putString("NODE_PRE_TYPE", nextFodeRowset.getString("NODE_TYPE", ""));
				}
				
				flowNodeDS = FlowManagerService.loadFlowNode(flowNodePO);
				
				//获取流程是否在结束流程列表中
				//获取当前流程信息
				BILL_WHERE = " BIZ_DATE = '" + rowset.getString("F_KJND", "") + "' and BIZ_DJBH = '" + rowset.getString("F_DJBH", "") + "' and "
				           + " FLOW_ID = 'PSHT_FLOW'";
				//判断是否为会签节点，决定是否加上部门条件
				if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
					BILL_WHERE += " and TASK_TO_UNIT = '" + user.getUSER_ORGID() + "'";
				}
				
				
				flowPO.SetValueByParamName("tableName", "FLOW_TASK_END_LIST");
				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
				flowDataSet = FCTService.getFCTRows(flowPO);
				//如果有数据，则表示流程已经结束
				if(flowDataSet.getRowCount() > 0) {
					flowRowset = flowDataSet.getRowSet(0);
					//modify by prayer 2014-11-03 因为编辑的时候打开有问题，留着也没用，清空掉了
					for(int i = 1; i < 10; i++) {
						flowRowset.putString("F_CHAR0" + i, "");
					}
					for(int i = 10; i <= 20; i++) {
						flowRowset.putString("F_CHAR" + i, "");
					}
					flowRowset.putString("OP_NOTE", "");
					model.addAttribute("endFlowObject", flowRowset);
				} 
				//说明流程没有结束
				else {
					model.addAttribute("endFlowObject", "");
					//获取当前流程信息
					BILL_WHERE = " BIZ_DATE = '" + rowset.getString("F_KJND", "") + "' and BIZ_DJBH = '" + rowset.getString("F_DJBH", "") + "' and "
					           + " FLOW_ID = 'PSHT_FLOW' and NODE_TAG = '" + nodeRowset.getString("NODE_ID", "") + "'";
					//判断是否为会签节点，决定是否加上部门条件
					if(nodeRowset.getString("NODE_TYPE", "").equals("countersign")) {
						BILL_WHERE += " and TASK_TO_UNIT = '" + user.getUSER_ORGID() + "'";
					}
					
					
					flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
					flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
					flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
					flowDataSet = FCTService.getFCTRows(flowPO);
					if(flowDataSet.getRowCount() > 0) {
						flowRowset = flowDataSet.getRowSet(0);
						//modify by prayer 2014-11-03 因为编辑的时候打开有问题，留着也没用，清空掉了
						for(int i = 1; i < 10; i++) {
							flowRowset.putString("F_CHAR0" + i, "");
						}
						for(int i = 10; i <= 20; i++) {
							flowRowset.putString("F_CHAR" + i, "");
						}
						flowRowset.putString("OP_NOTE", "");
						model.addAttribute("flowObject", flowRowset);
					}
					
					if(flowRowset != null) {
						//获取下一个流程信息
						BILL_WHERE = " BIZ_DATE = '" + rowset.getString("F_KJND", "") + "' and BIZ_DJBH = '" + rowset.getString("F_DJBH", "") + "' and "
						           + " FLOW_ID = 'PSHT_FLOW' and POP_ID = '" + flowRowset.getString("OP_ID", "") + "'";
						flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
						flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
						flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
						flowDataSet = FCTService.getFCTRows(flowPO);
						if(flowDataSet.getRowCount() > 0) {
							nextFlowRowset = flowDataSet.getRowSet(0);
						}		
					}	
					//如果流程结束
					if(flowRowset == null) {
						model.addAttribute("canSubmit", "0");
						model.addAttribute("canRetrieve", "0");
						model.addAttribute("canRollback", "0");
					}
					//如果当前流程状态为代办，则可以提交和编辑,如果是申请单生成的，就不能编辑
					else if(flowRowset.getString("RESR_STATUS", "").equals("pending")) {
						model.addAttribute("canEdit", "1");
						model.addAttribute("canSubmit", "1");
						model.addAttribute("canRetrieve", "0");
						model.addAttribute("canRollback", "0");
					}
					//如果已经处理过，则判断下一个流程，下一个流程如果为代办，则可以取回，否则不能取回
					else {
						if(nextFlowRowset.getString("RESR_STATUS", "").equals("pending")) {
							model.addAttribute("canEdit", "0");
							model.addAttribute("canSubmit", "0");
							if(flowRowset.getString("NODE_TAG", "").compareTo(nextFlowRowset.getString("NODE_TAG", "")) > 0) {
								model.addAttribute("canRetrieve", "0");
							} else {
								model.addAttribute("canRetrieve", "1");
							}
							model.addAttribute("canRollback", "0");
						} else {
							model.addAttribute("canEdit", "0");
							model.addAttribute("canSubmit", "0");
							model.addAttribute("canRetrieve", "0");
							model.addAttribute("canRollback", "0");
						}					
					}
				}
				
				jsonObject.putAll(nodeRowset);
				model.addAttribute("nodeRowSet", nodeRowset);
				model.addAttribute("jsonNodeRowSet", jsonObject.toString());
			} else {
				model.addAttribute("canEdit", "0");
				model.addAttribute("canSubmit", "0");
				model.addAttribute("canRetrieve", "0");
				model.addAttribute("canRollback", "0");
			}
			rowset.putString("F_CODE", user.getUSER_ORGID());
			model.addAttribute("dictObject", rowset);
			model.addAttribute("F_BMBH", user.getUSER_ORGID());
			model.addAttribute("dataset", dataset);			
			model.addAttribute("flowNodeObject", nodeRowset);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/cms/business/accreditationContract/form";
	}
	
	/**
	 * 评审合同删除
	 * @param model
	 * @return
	 */
	@RequestMapping("/accreditationContract/delete/{F_ZDBH}")
	public ModelAndView accreditationContractDelete(@PathVariable("F_ZDBH") String F_ZDBH) {	
		JResponseObject RO = DMTService.deleteRow("PSSXZD", F_ZDBH);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}
	
	/**
	 * 评审合同删除
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/invalidAccreditationContract/invalidForm", method=RequestMethod.POST)
	public  ModelAndView invalidForm(@RequestBody Map dataMap, Model model) throws Exception {  
		EFRowSet              rowSet = EMPJsonUtil.convertMapToRowSet(dataMap);
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();		
		SYSUser                  user = (SYSUser) session.getAttribute("contextUser");
		JParamObject           flowPO = JParamObject.Create();
		EFDataSet          flowNodeDS = null;
		EFRowSet           nodeRowset = EFRowSet.getInstance();
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "PSHT_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_TYPE", "start");
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);
		
		if(flowNodeDS == null || flowNodeDS.getRowCount() == 0) {
			return ajaxDoneError("提交失败！原因：<br>【" + user.getUSER_ID() + "-" + user.getUSER_NAME() + "】没有权限提交单据！");
		} else {
			nodeRowset = flowNodeDS.getRowSet(0);
		}
		
		rowSet.setDataMap(rowSet.getDataMap());
		rowSet.remove(PSHTMX.class.getName());
		rowSet.putString("F_DJZT", "0");
		rowSet.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEMS_ALL_);
		
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "CMSInvalidForm");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "PSHT_FLOW");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_BMBH, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER, user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
		
		EFFormDataModel formDataModel = formModelResolver.submitForm("PSHTXX", rowSet, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("提交失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowSet = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowSet.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowSet.getObject("F_DJBH", ""));
		return ajaxDoneSuccess("单据分录保存成功！单据编号【" + rowSet.getObject("F_DJBH", "") + "】");
	}
	
	/**
	 * 判断提交状态
	 * @param model
	 * @param headRowSet
	 * @param itemDataSet
	 * @param flowRowSet
	 */
	private void checkFormStatus(Model model, SYSUser user, EFRowSet headRowSet, EFDataSet itemDataSet, EFRowSet flowNodeRowSet, EFDataSet flowDataSet) {
		String             F_DJZT = "";
		String             F_FLZT = "";
		String         BILL_WHERE = "";
		JParamObject       flowPO = JParamObject.Create();
		EFRowSet       itemRowset = null;
		EFRowSet           rowSet = null;
		EFRowSet       flowRowset = null;
		
		//获取节点任务
		if(flowNodeRowSet != null) {
			F_DJZT = headRowSet.getString("F_DJZT", "0");
			headRowSet.putString("FLOW_NODE_NAME", flowNodeRowSet.getString("NODE_NAME", ""));
			//未提交
			if(F_DJZT.equals("0")) {
				model.addAttribute("canPrint", "0");
				model.addAttribute("canSubmit", "1");
				model.addAttribute("canRetrieve", "0");
			} 
			//已提交
			else if(F_DJZT.equals("1")) {
				for(int i = 0; i < itemDataSet.getRowCount(); i++) {
					F_FLZT = itemDataSet.getRowSet(i).getString("F_FLZT", "");
					if(F_FLZT.equals("1")) {
						model.addAttribute("canRetrieve", "0");
						return;
					}
				}
				model.addAttribute("canSubmit", "0");
				model.addAttribute("canPrint", "0");
				model.addAttribute("canRetrieve", "1");
			} 
			//退回
			else if(F_DJZT.equals("2")) {
				for(int i = 0; i < itemDataSet.getRowCount(); i++) {
					itemRowset = itemDataSet.getRowSet(i);
					if(flowDataSet.getRowSet(itemRowset.getString("F_BMBH", "")) != null) {
						itemRowset.putString("NODE_OP", "1");
					}
					else {
						itemRowset.putString("NODE_OP", "0");
					}
				}
				model.addAttribute("canSubmit", "1");
				model.addAttribute("canRetrieve", "0");
				model.addAttribute("canPrint", "0");
			} else if(F_DJZT.equals("3")) {
				model.addAttribute("canSubmit", "0");
				model.addAttribute("canRetrieve", "0");
				model.addAttribute("canPrint", "1");
			} else {
				model.addAttribute("canSubmit", "0");
				model.addAttribute("canPrint", "0");
				model.addAttribute("canRetrieve", "0");
			}
			BILL_WHERE = " BIZ_DATE = '" + headRowSet.getString("F_KJND", "") + "' and BIZ_DJBH = '" + headRowSet.getString("F_DJBH", "") + "' ";
			flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
			flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
			flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
			rowSet = FCTService.getFCTRow(flowPO);
			//开始节点并且待办任务中没有流程信息，单据状态不是作废状态，满足上面三个条件，才可以作废			
			if(rowSet == null && flowNodeRowSet != null && !F_DJZT.equals("4")) {
				model.addAttribute("canInvalid", "1");
			} else {
				model.addAttribute("canInvalid", "0");
			}
		} else {
			headRowSet.putString("FLOW_NODE_NAME", "提交任务");
			model.addAttribute("canSubmit", "0");
			model.addAttribute("canSubmit", "0");
			model.addAttribute("canPrint", "0");
			model.addAttribute("canRetrieve", "0");
			model.addAttribute("canInvalid", "0");
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 评审合同分录添加
	 * @param F_GUID
	 * @param EDIT_TYPE 为空的时候分录编辑按钮显示，为1的时候不显示编辑按钮
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/accreditationContract/itemList")
	public String accreditationContractItemList(@RequestParam String F_GUID, @RequestParam String EDIT_TYPE, Model model) throws Exception {	
		JParamObject           PO = JParamObject.Create();

		PO.SetValueByParamName("ENUM_TYPE", "2");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName("BILL_WHERE", " F_GUID = '" + F_GUID + "'");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		EFFormDataModel formDataModel = formModelUtil.loadForm(PO);
		model.addAttribute("dataset", formDataModel.getBillDataSet());

		model.addAttribute("F_GUID", F_GUID);
		model.addAttribute("EDIT_TYPE", EDIT_TYPE);
		return "/cms/business/accreditationContract/formItemList";
	}
}
